package com.ba.grain;

import java.util.ArrayList;
import java.util.List;

import util.Message;

import android.annotation.SuppressLint;
import android.app.ListActivity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.SparseBooleanArray;
import android.view.ActionMode;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.Checkable;
import android.widget.GridView;
import android.widget.ListView;

public class IMSActivity extends ListActivity implements OnClickListener {
	public static final String TAG = "RecordActivity";
	protected OnItemLongClickListener mOnItemLongClickListener;
	public ActionMode mActionMode;
	protected Button mBtn;

	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_record);

		mBtn = (Button) findViewById(R.id.btn_use);
		mBtn.setVisibility(View.GONE);
		mBtn.setOnClickListener(this);
		mBtn.setText("删除");

		LoginActivity.sShowNotify = false;
		
		IMSAdapter dua = new IMSAdapter(this, getListView());
		setListAdapter(dua);
		if (dua.isEmpty()) {
			getListView().setVisibility(View.GONE);
		}
		SharedPreferences pref = getSharedPreferences(LoginActivity.PREF_MESSAGE, MODE_PRIVATE);
		pref.registerOnSharedPreferenceChangeListener(dua);
		getListView().setChoiceMode(
				VERSION.SDK_INT >= VERSION_CODES.HONEYCOMB ? ListView.CHOICE_MODE_MULTIPLE_MODAL
						: ListView.CHOICE_MODE_NONE);
		if (VERSION.SDK_INT >= VERSION_CODES.HONEYCOMB) {
			getListView().setMultiChoiceModeListener(new MultiChoiceModeListener());
		} else {
			mOnItemLongClickListener = new AbsListView.OnItemLongClickListener() {
				@Override
				public boolean onItemLongClick(AdapterView<?> parent, View view, int position,
						long id) {

					if (!TextUtils.isEmpty(mBtn.getText())) {
						getListView().setOnLongClickListener(null);
						mBtn.setVisibility(View.VISIBLE);
						getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
					}

					return false;
				}
			};
			getListView().setOnItemLongClickListener(mOnItemLongClickListener);
		}

	}

	public static String NAME_RECORDS = "records";

	protected void clearListChoice() {
		mBtn.setVisibility(View.GONE);
		getListView().setOnItemLongClickListener(mOnItemLongClickListener);
		getListView().setChoiceMode(ListView.CHOICE_MODE_NONE);
		getListView().clearChoices();
		for (int i = 0; i < getListView().getChildCount(); i++) {
			View c = getListView().getChildAt(i);
			if (c instanceof Checkable) {
				((Checkable) c).setChecked(false);
			}
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.record, menu);
		return true;
	}

	@SuppressLint("NewApi")
	public class MultiChoiceModeListener implements GridView.MultiChoiceModeListener {
		public boolean onCreateActionMode(ActionMode mode, Menu menu) {
			mode.setTitle("选中项目");
			mActionMode = mode;

			if (!TextUtils.isEmpty(mBtn.getText())) {
				mBtn.setVisibility(View.VISIBLE);
			}
			return true;
		}

		public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
			return true;
		}

		public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
			return true;
		}

		public void onDestroyActionMode(ActionMode mode) {
			mActionMode = null;
			mBtn.setVisibility(View.GONE);
		}

		public void onItemCheckedStateChanged(ActionMode mode, int position, long id,
				boolean checked) {
			int selectCount = getListView().getCheckedItemCount();
			mode.setSubtitle(String.format("%d项", selectCount));
		}

	}

	@Override
	protected void onDestroy() {
		SharedPreferences pref = getSharedPreferences(LoginActivity.PREF_MESSAGE, MODE_PRIVATE);
		IMSAdapter dua = (IMSAdapter) getListAdapter();
		pref.unregisterOnSharedPreferenceChangeListener(dua);
		LoginActivity.sShowNotify = true;
		super.onDestroy();
	}

	@SuppressLint("NewApi")
	@Override
	public void onClick(View v) {
		if (mBtn == v) {
			SparseBooleanArray sba = getListView().getCheckedItemPositions();
			List<Message> items = new ArrayList<Message>();
			for (int i = 0; i < sba.size(); i++) {
				int key = sba.keyAt(i);
				if (sba.get(key)) {
					Message li = (Message) getListView().getItemAtPosition(key);
					items.add(li);
				}
			}
			SharedPreferences pref = getSharedPreferences(LoginActivity.PREF_MESSAGE,
					Context.MODE_PRIVATE);
			LoginActivity.removeMessageFromLocal(items, pref);
			IMSAdapter adapter = (IMSAdapter) getListAdapter();
			adapter.notifyDataSetChanged();
			if (VERSION.SDK_INT >= VERSION_CODES.HONEYCOMB) {
				mActionMode.finish();
			} else {
				// clearListChoice();
			}
		}
	}
}
